home *** CD-ROM | disk | FTP | other *** search
/ MacFormat 1999 Spring / macformat-077.iso / Shareware Plus / Development / Akua Sweets 131 / Akua Sweets Examples / File / HTML Prep < prev    next >
Encoding:
Text File  |  1999-03-04  |  6.5 KB  |  281 lines  |  [TEXT/ToyS]

  1. -- Preferences
  2. property kasPrefix : "<!! (c) 1998 AKUA interactive media AG - GLK OPO %v %d !!>" -- Set to empty string to prevent prefixing
  3. property kasPrefName : "Unix Textify"
  4. property kasPrefixExt : {".html", ".htm"} -- Only prefix these files
  5.  
  6. property kasLowerCaseNames : true
  7.  
  8. property kasTypesToDo : {"TEXT"} -- Only run on these types
  9. property kasExtToDo : "" -- Use non-empty match string (e.g. "*.html") to limit to that extension
  10.  
  11. property quote : "•"
  12. property cr : (ASCII character 13)
  13. property lf : (ASCII character 10)
  14. property crlf : cr & lf
  15.  
  16. -- Globals
  17. global gasInfoWind -- Info window
  18. global gasInfoPos -- Position of info window
  19. global gasFoldersToDo -- The folders left to process
  20. global gasConverted -- Number gone!
  21. global gasChecked -- Number checked!
  22. global gasPrefix -- Adjusted kasPrefix (munge the % parms)
  23.  
  24.  
  25. on open fsObjs
  26.     -- Load prefs, show window
  27.     pfLoad()
  28.     
  29.     -- Set up prefix
  30.     set gasPrefix to munge kasPrefix ¬
  31.         searching for ¬
  32.         "%d" replacing it with (the clock in extended sortable form)
  33.     
  34.     set gasPrefix to munge gasPrefix ¬
  35.         searching for ¬
  36.         "%v" replacing it with "V1.10"
  37.     
  38.     
  39.     set gasConverted to 0
  40.     set gasChecked to 0
  41.     
  42.     set gasInfoWind to display info titled kasPrefName ¬
  43.         located at gasInfoPos ¬
  44.         message "Scanning…"
  45.     
  46.     -- Do files
  47.     set gasFoldersToDo to {}
  48.     
  49.     repeat with fsObj in fsObjs
  50.         set myInfo to (basic info for fsObj)
  51.         
  52.         if (system type of myInfo is "fold") then
  53.             set gasFoldersToDo to gasFoldersToDo & {fsObj}
  54.         else if (system type of myInfo is in kasTypesToDo) then
  55.             if (kasExtToDo is "") or ((collect lines of (catalog name of myInfo) that match kasExtToDo) is not "") then
  56.                 DoOne(fsObj)
  57.             end if
  58.         end if
  59.     end repeat
  60.     
  61.     -- Do folders
  62.     repeat while gasFoldersToDo is not {}
  63.         -- Pop one off the end
  64.         set n to the number of items of gasFoldersToDo
  65.         set fsObj to item n of gasFoldersToDo
  66.         
  67.         if (n > 1) then
  68.             set gasFoldersToDo to items 1 through (n - 1) of gasFoldersToDo
  69.         else
  70.             set gasFoldersToDo to {}
  71.         end if
  72.         
  73.         display info gasInfoWind ¬
  74.             message ("Folders to go: " & n) ¬
  75.             at line 6 ¬
  76.             using color (15 * 32)
  77.         
  78.         -- Process it
  79.         GoDeep(fsObj)
  80.     end repeat
  81.     
  82.     display info gasInfoWind message "DONE!"
  83.     
  84.     pause for 5 with seconds timing -- Let screen wait...
  85.     
  86.     set gasInfoPos to screen location of ¬
  87.         (display info gasInfoWind with disposal)
  88.     
  89.     pfSave() -- Save window location
  90. end open
  91.  
  92.  
  93. on DoOne(fsObj)
  94.     set aFileInfo to (alias info from fsObj)
  95.     
  96.     display info gasInfoWind ¬
  97.         message "File: " & (original name of aFileInfo) ¬
  98.         at line 2
  99.     
  100.     set gasChecked to gasChecked + 1
  101.     
  102.     display info gasInfoWind ¬
  103.         message ("Checked: " & gasChecked) ¬
  104.         at line 7 ¬
  105.         using color 15
  106.     
  107.     ConvertFile(fsObj)
  108. end DoOne
  109.  
  110.  
  111. on ConvertFile(fsObj)
  112.     try
  113.         set fileRef to ¬
  114.             open fork from fsObj ¬
  115.                 with write access
  116.     on error errStr
  117.         display info gasInfoWind ¬
  118.             message ("Error: " & errStr) ¬
  119.             at line 12 ¬
  120.             using color (25 * 1024)
  121.         return
  122.     end try
  123.     
  124.     set fname to catalog name of (basic info for fsObj)
  125.     
  126.     set fileData to read data from fileRef
  127.     set newData to fileData
  128.     
  129.     -- Convert DOS to Unix
  130.     set newData to munge newData ¬
  131.         searching for crlf ¬
  132.         replacing it with lf
  133.     
  134.     -- Convert Mac to Unix
  135.     set newData to munge newData ¬
  136.         searching for cr ¬
  137.         replacing it with lf
  138.     
  139.     -- Rename to lowercase?
  140.     set changed to false
  141.     if (kasLowerCaseNames) then
  142.         set flow to transcribe fname with «class ñLow»
  143.         considering case
  144.             if flow is not fname then
  145.                 collate fsObj renaming it to flow
  146.                 set changed to (kasPrefix is not "") -- Prefix if this is a change
  147.             end if
  148.         end considering
  149.     end if
  150.     
  151.     if changed or (newData is not fileData) then
  152.         -- Check prefix
  153.         if (kasPrefix is not "") then
  154.             -- Does this file get prefixes?
  155.             set doThis to (kasPrefixExt is {})
  156.             
  157.             repeat with suffix in kasPrefixExt
  158.                 display info gasInfoWind ¬
  159.                     message (the text from character -(length of suffix) to -1 of fname) ¬
  160.                     at line 15
  161.                 
  162.                 display info gasInfoWind ¬
  163.                     message suffix ¬
  164.                     at line 18
  165.                 
  166.                 set fext to (the text from character -(length of suffix) to -1 of fname)
  167.                 set os to offset of fext in suffix
  168.                 
  169.                 -- A check of equality fails here - I don't know why the hell?!?
  170.                 -- if (fext is suffix) then
  171.                 if ((offset of fext in suffix) is 1) then set doThis to true
  172.             end repeat
  173.             
  174.             if (doThis) then
  175.                 display info gasInfoWind ¬
  176.                     message ("Prefix?") ¬
  177.                     at line 16
  178.                 
  179.                 -- Lop off current prefix
  180.                 if (the text from character 1 to 4 of kasPrefix) is (the text from character 1 to 4 of newData) then
  181.                     set n to offset of cr in newData
  182.                     if (n is 0) then set n to offset of lf in newData
  183.                     if n is not 0 then
  184.                         set newData to gasPrefix & (the text from character n to -1 of newData)
  185.                         display info gasInfoWind ¬
  186.                             message ("Updated prefix") ¬
  187.                             at line 17
  188.                     end if
  189.                 else
  190.                     set newData to gasPrefix & lf & newData
  191.                     
  192.                     display info gasInfoWind ¬
  193.                         message ("Adding prefix") ¬
  194.                         at line 17
  195.                 end if
  196.             else
  197.                 display info gasInfoWind ¬
  198.                     message ("No Prefix") ¬
  199.                     at line 16
  200.             end if
  201.         end if
  202.         
  203.         -- Reset file to 0 long
  204.         size fork fileRef ¬
  205.             given «class len »:0
  206.         
  207.         -- Write data
  208.         write data to fileRef ¬
  209.             from buffer newData
  210.         
  211.         set gasConverted to gasConverted + 1
  212.         
  213.         display info gasInfoWind ¬
  214.             message ("Converted: " & gasConverted) ¬
  215.             at line 8 ¬
  216.             using color (15 * 1024)
  217.         display info gasInfoWind ¬
  218.             message ("Last: " & fname) ¬
  219.             at line 9 ¬
  220.             using color (16 * 1024)
  221.     end if
  222.     
  223.     close fork fileRef
  224. end ConvertFile
  225.  
  226.  
  227.  
  228. on GoDeep(foldObj)
  229.     display info gasInfoWind ¬
  230.         message "Path: " & (foldObj as string)
  231.     
  232.     set daddy to foldObj as string
  233.     
  234.     -- Do kinds that match
  235.     display info gasInfoWind ¬
  236.         message "Scanning files" at line 5
  237.     
  238.     if (kasExtToDo is "") then
  239.         set myItems to the entries in foldObj ¬
  240.             whose types are in kasTypesToDo
  241.     else
  242.         set myItems to the entries in foldObj ¬
  243.             whose types are in kasTypesToDo ¬
  244.             whose names match kasExtToDo
  245.     end if
  246.     
  247.     repeat with myItem in myItems
  248.         DoOne((daddy & myItem) as alias)
  249.     end repeat
  250.     
  251.     -- Do folders
  252.     display info gasInfoWind ¬
  253.         message "Scanning subfolders" at line 5
  254.     
  255.     set myItems to the entries in foldObj ¬
  256.         whose kinds are a folder
  257.     
  258.     repeat with myItem in myItems
  259.         set gasFoldersToDo to gasFoldersToDo & {(daddy & myItem) as alias}
  260.     end repeat
  261.     
  262.     -- Done
  263.     display info gasInfoWind ¬
  264.         message "…" at line 5
  265. end GoDeep
  266.  
  267.  
  268. on pfLoad()
  269.     try
  270.         set ourPrefs to (load preference named kasPrefName)
  271.         set gasInfoPos to item 1 of ourPrefs
  272.     on error
  273.         set gasInfoPos to {0, 0}
  274.     end try
  275. end pfLoad
  276.  
  277.  
  278. on pfSave()
  279.     save preference {gasInfoPos} named kasPrefName
  280. end pfSave
  281.